// pages/baby/add/add.js
//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js');
const date = require('../../../utils/date.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    host:app.globalData.host,
    sexPicker:['男','女'],
    sex:0,
    birth:'2010-01-01',
    image:'',
    modalName:'',
  },
  BackPage:function(){
    wx.redirectTo({
      url: '/pages/baby/index/index',
    })
  },
  showModal:function(){
    this.setData({
      modalName: 'modal1',
    })
  },
  hideModal:function(){
    this.setData({
      modalName: '',
    })
  },
  /**
   * 获取三个月之前的时间
   */
  beforeMonth: function () {
    var month = new Date().getMonth();
    var time = date.formatDate(new Date(new Date().setMonth(month-3))) ;
    this.setData({
      before_measure_time: time
    })
  },
  sexChange(e) {
    this.setData({
      sex: e.detail.value
    })
  },
  birthChange(e) {
    this.setData({
      birth: e.detail.value
    })
  },
  measureChange(e) {
    this.setData({
      before_measure_time: e.detail.value
    })
  },
  /**
   * 上传图片
   * @param callback
   */
  ChooseImage: function () {
    var _this = this;
    util.uploadImage(function (res) {
      if(res.code == 1){
        console.log
        _this.setData({
          image: res.data[0]['path'],
          full_image: app.globalData.host + res.data[0]['path'],
        });
      }else{
        util.fail(res.msg,_this)
      }
    })
  },

  delete: function (e) {
    var that = this;
    util.request('baby/delete',{id:this.data.id},function (data) {
      if(data.code == 1){
        if(app.globalData.parent){
          app.globalData.parent.baby_default_id = data.data.baby_default_id;
        }
        console.log( data.data.baby_default_id);
        util.success('删除成功');
        setTimeout(function(){
          wx.redirectTo({
            url: '/pages/baby/index/index',
          })
        },1500)

      }else{
        util.fail(data.msg,that);
      }
    });
  },
  submit: function (e) {
    var data = e.detail.value;
    console.log(e.detail.value);
    var that = this
    data.image = that.data.image;
    data.id = that.data.id;
    data.birth = that.data.birth;
    data.sex = that.data.sex;
    if (data.image == ''){
      util.fail("请上传宝贝图片", that)
      return
    }
    if (data.name == ''){
      util.fail("请输入宝贝姓名", that)
      return
    }
    if (data.farther_height == '') {
      util.fail("请输入父亲身高", that)
      return
    }
    if (data.mother_height == '') {
      util.fail("请输入母亲身高", that)
      return
    }
    util.request('baby/update',data,
        function (data) {
          if(data.code == 1){
            if(app.globalData.parent){
              app.globalData.parent.baby_default_id = data.data.baby_default_id;
            }
            console.log( data.data.baby_default_id);
            util.success();
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/baby/index/index',
              })
            },1500)

          }else{
            util.fail(data.msg,that);
          }
        });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.beforeMonth();
    var id = e.id;
    var that = this;
    that.setData({
      id:id
    })
    util.request('baby/edit', {id:id}, function (data) {
      if(data.code == 1){
        that.setData({
          baby:data.data.data,
          sex:data.data.data.sex,
          birth:data.data.data.birth,
          image:data.data.data.image,
          full_image: app.globalData.host + data.data.data.image,
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
